home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 398 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: gryphon.phoenix.net!usenet
  2. From: brucew@phoenix.net (Bruce Wedding)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is '?' in C mean....?????
  5. Date: Fri, 05 Jan 1996 01:34:43 GMT
  6. Organization: BranPaul Systems
  7. Message-ID: <4chsrp$1sf@gryphon.phoenix.net>
  8. References: <4cgsa8$bm2@wumpus.cc.uow.edu.au>
  9. NNTP-Posting-Host: dial101.phoenix.net
  10. X-Newsreader: Moe's Newsreader    
  11.  
  12. tp86@wumpus.cc.uow.edu.au (PAOPENG THEERADECH) wrote:
  13.  
  14. > The codes that I saw are;
  15. >        max = x>y ? x:y;
  16. >         
  17. >            and
  18. >
  19. >        printf("%d", x>y ? x:y);
  20. >
  21. >Could anyone here explain to me what is "?" means and what the purpose of using
  22. >it???. 
  23.  
  24. It is the ternary operator.  It evaluates the expression before the ?
  25. and if true, executes the code following the ?, if false, it executes
  26. teh code following the :
  27.  
  28. Your max example could be rewritten like this:
  29.  
  30. max = x>y ? x:y;
  31.  
  32. if (x>y)
  33.    max = x;
  34. else
  35.    max = y;
  36.  
  37.  
  38. Bruce D. Wedding                        Have Compiler, Will Travel!
  39.               Perspicacious Progamming Performed Promptly
  40. Katy, Texas, USA, Planet Earth, Milkyway Galaxy, Known Universe
  41.  
  42.